import request from '../utils/request'
import type { ApiResponse } from './user'

export interface ChatMessage {
    id: number;
    conversationId: number;
    senderId: number;
    receiverId: number;
    content: string;
    type: 'text' | 'image' | 'product'; // 消息类型
    productInfo?: any; // 如果是商品卡片，则包含商品信息
    createdAt: string;
    status: 'sent' | 'delivered' | 'read';
}

export interface ChatConversation {
    id: number;
    partnerId: number;
    partnerName: string;
    partnerAvatar: string;
    lastMessage: ChatMessage;
    unreadCount: number;
    updatedAt: string;
    school?: string;
    phone?: string;
    region?: string;
    introduction?: string;
    productId?: number;
    productTitle?: string;
    productThumbnail?: string;
}

/**
 * 获取聊天会话列表
 * @param params 分页参数
 * @returns Promise<ApiResponse<{ content: ChatConversation[], totalElements: number }>>
 */
export async function getChatConversations(params: { page: number, pageSize: number, userId: number }): Promise<ApiResponse<{ content: ChatConversation[], totalElements: number }>> {
    return request.get<ApiResponse<{ content: ChatConversation[], totalElements: number }>>('/chat/conversations', { params });
}

/**
 * 获取与指定用户的聊天记录
 * @param params 聊天记录参数
 * @returns Promise<ApiResponse<{ content: ChatMessage[], totalElements: number }>>
 */
export async function getChatMessages(params: { userId: number, conversationId: number, page?: number, pageSize?: number }): Promise<ApiResponse<{ content: ChatMessage[], totalElements: number }>> {
    return request.get<ApiResponse<{ content: ChatMessage[], totalElements: number }>>('/chat/messages', { params });
}

/**
 * 发送消息
 * @param data 消息数据
 * @returns Promise<ApiResponse<ChatMessage>>
 */
export async function sendChatMessage(data: { userId: number, conversationId: number, receiverId: number, content: string, type: string }): Promise<ApiResponse<ChatMessage>> {
    return request.post<ApiResponse<ChatMessage>>('/chat/send', data);
}

/**
 * 发送商品卡片
 * @param data 商品卡片数据
 * @returns Promise<ApiResponse<ChatMessage>>
 */
export async function sendProductCard(data: { userId: number, conversationId: number, receiverId: number, productId: number }): Promise<ApiResponse<ChatMessage>> {
    return request.post<ApiResponse<ChatMessage>>('/chat/send-product', data);
}

/**
 * 标记会话已读
 * @param data 请求参数
 * @returns Promise<ApiResponse<null>>
 */
export async function markConversationAsRead(data: { userId: number, conversationId: number }): Promise<ApiResponse<null>> {
    return request.put<ApiResponse<null>>(`/chat/mark-read?userId=${data.userId}&conversationId=${data.conversationId}`);
}

/**
 * 删除会话
 * @param data 请求参数
 * @returns Promise<ApiResponse<null>>
 */
export async function deleteConversation(data: { userId: number, conversationId: number }): Promise<ApiResponse<null>> {
    return request.delete<ApiResponse<null>>(`/chat/delete-conversation?userId=${data.userId}&conversationId=${data.conversationId}`);
}

/**
 * 创建新会话
 * @param data 请求参数
 * @returns Promise<ApiResponse<ChatConversation>>
 */
export async function createConversation(data: { userId: number, partnerId: number }): Promise<ApiResponse<ChatConversation>> {
    return request.post<ApiResponse<ChatConversation>>('/chat/create-conversation', data);
}

/**
 * 获取未读消息数量
 * @param params 请求参数
 * @returns Promise<ApiResponse<{ totalUnread: number, conversationCounts: Array<{ conversationId: number, count: number }> }>>
 */
export async function getUnreadMessageCount(params: { userId: number }): Promise<ApiResponse<{ totalUnread: number, conversationCounts: Array<{ conversationId: number, count: number }> }>> {
    return request.get<ApiResponse<{ totalUnread: number, conversationCounts: Array<{ conversationId: number, count: number }> }>>('/chat/unread-count', { params });
} 